package com.mql.ganio.ui.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.mql.ganio.MainActivity;
import com.mql.ganio.R;
import com.mql.ganio.ui.base.BaseFragment;
import com.mql.ganio.ui.presenter.EveryDayAtPresenter;
import com.mql.ganio.ui.view.IGanHuoAtView;
import com.mql.ganio.util.LogUtils;
import com.youth.banner.Banner;

import butterknife.Bind;


/**
 * @创建者 mql
 * @描述 每日推荐fragment
 */

public class EverydayFragment extends BaseFragment<IGanHuoAtView, EveryDayAtPresenter> implements IGanHuoAtView {
    @Bind(R.id.xrv_custom)
    XRecyclerView mXrvCustom;
    View mHeaderView;
    Banner banner;

    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        mXrvCustom.setPullRefreshEnabled(false);
        mXrvCustom.setLoadingMoreEnabled(false);
        if (mHeaderView == null) {
            mHeaderView = View.inflate(getContext(), R.layout.header_item_everyday, null);
            banner = (Banner) mHeaderView.findViewById(R.id.banner);
            mXrvCustom.addHeaderView(mHeaderView);
        }
        mXrvCustom.setLayoutManager(new LinearLayoutManager(getContext()));
        // 需加，不然滑动不流畅
        mXrvCustom.setNestedScrollingEnabled(false);
        mXrvCustom.setHasFixedSize(false);
        mXrvCustom.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void initData() {
        super.initData();
        // 显示时轮播图滚动
        if (mPresenter != null && banner != null) {
            banner.startAutoPlay();
           banner.setDelayTime(4000);
        }
        mPresenter.getBannerData();
        mPresenter.refreshData();
    }

    @Override
    protected void onInvisible() {
        // 不可见时轮播图停止滚动
        if (banner != null) {
            banner.stopAutoPlay();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        // 失去焦点，否则RecyclerView第一个item会回到顶部
        mXrvCustom.setFocusable(false);
        LogUtils.e("-----EverydayFragment----", "onResume()");
        // 开始图片请求
        Glide.with(getActivity()).resumeRequests();
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtils.e("-----EverydayFragment", "----onPause()");
        // 停止全部图片请求 跟随着Activity
        Glide.with(getActivity()).pauseRequests();

    }

    @Override
    protected EveryDayAtPresenter createPresenter() {
        return new EveryDayAtPresenter((MainActivity) getActivity());
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_custom;
    }

    @Override
    public XRecyclerView getXRecyclerView() {
        return mXrvCustom;
    }

    @Override
    public Banner getBanner() {
        return banner;
    }
}
